import json
import kerlstor_disk_proxy
import kerlstor_queue_proxy
import battlefield_domain

class data(object):
    def __init__(self, stor_url, mq_url):
        self.stor = kerlstor_disk_proxy.disk_proxy(stor_url)
        self.mq = kerlstor_queue_proxy.queue_proxy(mq_url)
        self.player_prefix = "battlefield_player_"
        self.player_list = "battlefield.playerlist"
        self.client_id = "battlefield_rulai"
        self.action_prefix = "battlefield_action_"
    
    def update_player(self, player):
        self.stor.upload(self.player_prefix + player.name, player.serialize())
        
    def get_player(self, name):
        return battlefield_domain.deserialize_player(
            self.stor.download(self.player_prefix + name))
    
    def get_action(self, name):
        return battlefield_domain.deserialize_action(
            self.mq.fetch(self.action_prefix + name, self.client_id))
    
    def get_player_map(self):
        raw_data = self.stor.download(self.player_list)
        return json.read(raw_data)
        
